<?php
namespace App\Http\Controllers\Admin;

use App\Services\Admin\QrService;
use Illuminate\Http\Request;

class QrController extends BaseController
{
    public function __construct(QrService $qr)
    {
        $this->cors();
        $this->db = $qr;
    }

    /**
     * 码列表
     */
    public function getIndex(Request $request)
    {
//        header('Access-Control-Allow-Origin: *');
//        $headers = $this->getAid($request);
//        if ($headers['code'] != 0) {
//            return $this->toJson(-2, $headers['msg']);
//        }
        $pages['start'] = $request->get('pageStart', 0);
        $pages['limit'] = $request->get('pageLimit', 10);
        $pas['isuse'] = $request->post('isuse', 0); //1未使用2已使用
        $pas['scene_id'] = $request->post('scene_id', 0);
        $rst = $this->db->getList($pages, $pas);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data'], $rst['total']);
    }

    /**
     * 扫码记录
     */
    public function getLogIndex(Request $request)
    {
//        header('Access-Control-Allow-Origin: *');
//        $headers = $this->getAid($request);
//        if ($headers['code'] != 0) {
//            return $this->toJson(-2, $headers['msg']);
//        }
        $ucode = $request->post('ucode', '');
        $code = $request->post('code', '');
        $pages['start'] = $request->post('pageStart', 0);
        $pages['limit'] = $request->post('pageLimit', 10);
        $rst = $this->db->getLogList($code, $ucode, $pages);
        if ($rst['code']!=0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0, '', $rst['data']);
    }

    /**
     * 码生成
     */
    public function setAdd(Request $request)
    {
//        header('Access-Control-Allow-Origin: *');
//        $headers = $this->getAid($request);
//        if ($headers['code'] != 0) {
//            return $this->toJson(-2, $headers['msg']);
//        }
        //参数验证
        $this->validate($request, [
            'num' => 'required',
        ]);
        $num = $request->post('num');
        $rst = $this->db->batchAdd($num);
        if ($rst['code']!=0) {
            return $this->toJson(-2, $rst['msg']);
        }
        return $this->toJson(0);
    }

    /**
     * 补全showcode
     */
    public function modifyShowCode()
    {
        header('Access-Control-Allow-Origin: *');
        return $this->toJson(0, '', $this->db->modifyShowCode());
    }
}